﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace System.Windows.Forms
{
    /// <summary>
    /// 自定义控件之背景半透明遮罩加载控件
    /// </summary>
    [ToolboxBitmap(typeof(MaskLoading))]
    public partial class MaskLoading : Control
    {
        private bool _isTransparent = true;//是否透明  
        [Category("Transparent"), Description("是否使用透明,默认为True")]
        public bool IsTransparent
        {
            get { return _isTransparent; }
            set { _isTransparent = value; }
        }

        private int _alpha = 125;//设置透明度  
        [Category("Transparent"), Description("设置透明度")]
        public int Alpha
        {
            get { return _alpha; }
            set { _alpha = value; }
        }

        private Control _byControl;
        [Category("Transparent"), Description("要绑定的控件")]
        public Control ByControl
        {
            get { return _byControl; }
            set
            {
                _byControl = value;
                if (_byControl != null)
                {
                    _byControl.Controls.Add(this);
                }
            }
        }

        public MaskLoading() : this(125, true)
        {
            InitializeComponent();
        }

        /// <summary>  
        /// 初始化加载控件  
        /// </summary>  
        /// <param name="alpha">透明度</param>  
        /// <param name="isShowLoadingImage">是否显示加载图片</param>  
        public MaskLoading(int alpha, bool isShowLoadingImage)
        {
            SetStyle(ControlStyles.Opaque, true);//设置背景透明  
            base.CreateControl();
            _alpha = alpha;
            if (isShowLoadingImage)
            {
                PictureBox pic = new PictureBox();
                pic.BackColor = Color.White;
                pic.Image = AisinGioro.Core.Properties.Resources.Loading;
                pic.Size = new Size(32, 32);
                pic.SizeMode = PictureBoxSizeMode.AutoSize;
                pic.Location = new Point(this.Location.X + (this.Width - pic.Width) / 2,
                    this.Location.Y + (this.Height - pic.Height) / 2);//居中  
                pic.Anchor = AnchorStyles.None;
                this.Controls.Add(pic);
            }
            this.Visible = false;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; // 开启 WS_EX_TRANSPARENT,使控件支持透明  
                return cp;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Pen labelBorderPen;
            SolidBrush labelBackColorBrush;
            if (_isTransparent)
            {
                Color cl = Color.FromArgb(_alpha, this.BackColor);
                labelBorderPen = new Pen(cl, 0);
                labelBackColorBrush = new SolidBrush(cl);
            }
            else
            {
                labelBorderPen = new Pen(this.BackColor, 0);
                labelBackColorBrush = new SolidBrush(this.BackColor);
            }
            base.OnPaint(pe);
            pe.Graphics.DrawRectangle(labelBorderPen, 0, 0, this.Width, this.Height);
            pe.Graphics.FillRectangle(labelBackColorBrush, 0, 0, this.Width, this.Height);
        }

        /// <summary>  
        /// 显示加载  
        /// </summary>
        public void ShowLoad()
        {
            this.BringToFront();
            this.Dock = DockStyle.Fill;
            this.Visible = true;
        }

        /// <summary>  
        /// 隐藏加载  
        /// </summary>  
        public void HideLoad()
        {
            this.Visible = false;
        }
    }
}
